package edu.spec.student.dao.impl;

import edu.spec.student.dao.iface.IStudentLectureDao;
import edu.spec.student.model.Student;
import edu.spec.student.model.lecture.Lecture;
import edu.spec.student.model.lecture.LectureDto;
import edu.spec.student.model.lecture.StudentLecture;
import edu.spec.basic.model.enumeration.AttendStatus;
import org.hibernate.Query;
import org.hibernate.Session;
import org.madgecko.basic.dao.BaseDao;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * Created by MadFrog on 2017/6/12.
 */
@Repository
public class StudentLectureDao extends BaseDao<StudentLecture> implements IStudentLectureDao {

//    /**
//     * 正在进行的讲座。根据学生获取已报名，且时间已到（已开始，未结束）的讲座
//     * @param studentId 学生主键
//     * @return
//     */
//    @Override
//    public List<Lecture> listUnAttendLecture(Integer studentId) {
//        String hql = " select sl.lecture from StudentLecture sl where sl.student.id=" + studentId + " and sl.lecture.startTime<:currentDate and sl.lecture.endTime>:currentDate order by sl.lecture.startTime desc";
//        Session s = this.getSession();
//        Query query = s.createQuery(hql.toString());
//        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
//        List<Lecture> attendList = query.list();
//        return attendList;
//    }

    @Override
    public List<Integer> listExpectedLectureIds(Integer studentId) {
        String hql = " select sl.lecture.id from StudentLecture sl where sl.student.id=" + studentId + " and sl.lecture.startTime>:currentDate";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
        List<Integer> ids = query.list();
        return ids;
    }

    /**
     * 根据学生获取已报名，正在进行（已开始，未结束）的讲座
     * @param studentId 学生主键
     * @return
     */
    @Override
    public List<LectureDto> listUnderwayLectureDto(Integer studentId){
        // order by sl.lecture.startTime desc
        String hql = " select new edu.spec.sms.model.lecture.LectureDto(sl.lecture, sl.signIn) from StudentLecture sl where sl.student.id=" + studentId + " and sl.lecture.startTime<:currentDate and sl.lecture.endTime>:currentDate";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
        List<LectureDto> underwayList = query.list();
        return underwayList;
    }

    /**
     * 查询某个同学进行中、已经签到的讲座
     * @param studentId 学生主键
     * @return
     */
    @Override
    public List<Lecture> listUnderwaryAndSignInedLecture(Integer studentId) {
        String hql = " select sl.lecture from StudentLecture sl where sl.student.id=" + studentId + " and sl.lecture.startTime<:currentDate and sl.lecture.endTime>:currentDate and sl.signIn=1 order by sl.lecture.startTime desc";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
        List<Lecture> attendList = query.list();
        return attendList;
    }

    /**
     * 过去的讲座。根据学生获取已报名，且已听的讲座（时间已过）
     * @param studentId 学生主键
     * @return
     */
    @Override
    public List<Lecture> listAttendedLecture(Integer studentId) {
        String hql = " select sl.lecture from StudentLecture sl where sl.student.id=" + studentId + " and sl.lecture.endTime<:currentDate order by sl.lecture.startTime desc";
        Session s = this.getSession();
        Query query = s.createQuery(hql.toString());
        query.setTimestamp("currentDate", new Date());//注意这里，由于要保留时分秒，用setTimestamp，不要用setDate
        List<Lecture> attendList = query.list();
        return attendList;
    }

    @Override
    public void signIn(Integer studentId, Integer lectureId, Float latitude, Float longitude) {
        String hql = "update StudentLecture sl set sl.signIn=1, sl.latitude=" + latitude + ", sl.longitude=" + longitude + " where sl.student.id='" + studentId + "' and sl.lecture.id=" + lectureId;
        this.updateByHql(hql);
    }

//    @Override
//    public StudentLecture load(String openId, Integer eventId) {
//        String hql = "select sl from StudentLecture se where sl.student.openId='" + openId + "' and sl.event.id=" + eventId;
//        return (StudentLecture)this.queryObject(hql);
//    }

    @Override
    public void deleteByLecture(Integer lectureId) {
        String hql = "delete StudentLecture sl where sl.lecture.id=" + lectureId;
        this.updateByHql(hql);
    }

    @Override
    public List<Student> listStudentByCon(Integer lectureId, Integer clazzId, AttendStatus attendStatus) {
        String hql = "select sl.student from StudentLecture sl where sl.lecture.id=" + lectureId + " and sl.student.bjid=" + clazzId;

        // 未报名
        if (attendStatus==AttendStatus.UNENROLL){
            hql = "select sl.student.id from StudentLecture sl where sl.lecture.id=" + lectureId + " and sl.student.bjid=" + clazzId;
            List<Integer> enrollIds = this.listIdByHql(hql);
            hql = "from Student s where s.bjid=" + clazzId + " and s.id not in(:?)";
            System.out.println(hql);
            return this.list(hql, enrollIds);
        }
        //未签到
        else if (attendStatus==AttendStatus.UNSIGNIN){
            hql += " and sl.signIn<>1";
        }
        //正常参加
        if (attendStatus==AttendStatus.ATTEND){
            hql += " and sl.signIn=1";
        }
        return this.list(hql);
    }

    @Override
    public List<Student> listStudentByCon(Integer lectureId) {
        String hql = "";
        // 1.未报名
        hql = "from Student s where s.id not in(select sl.student.id from StudentLecture sl where sl.lecture.id=" + lectureId + ")";
        System.out.println(hql);
        List<Student> unAttendStudent = this.list(hql);
        for(Student s : unAttendStudent){
            s.setAttendStatus(AttendStatus.UNENROLL);
        }
        // 2.未签到
        hql = "select sl.student from StudentLecture sl where sl.lecture.id=" + lectureId + " and sl.signIn=null";
        List<Student> unsignInStudent = this.list(hql);
        for(Student s : unsignInStudent){
            s.setAttendStatus(AttendStatus.UNSIGNIN);
        }
        // 3.正常参加
        hql = "select sl.student from StudentLecture sl where sl.lecture.id=" + lectureId + " and sl.signIn=1";
        List<Student> attendStudent = this.list(hql);
        for(Student s : attendStudent){
            s.setAttendStatus(AttendStatus.ATTEND);
        }
        unAttendStudent.addAll(unsignInStudent);
        unAttendStudent.addAll(attendStudent);
        return unAttendStudent;
    }

    public void countAttend(Integer eventId){
        // 分专业统计参加人数
        String hql = " select new edu.spec.sms.model.event.SumDto(se.student.clazz.major.name, count(*)) from StudentEvent se where se.event.id=1 group by se.student.clazz.major";
        List major = this.list(hql);
        System.out.println(major.size());
    }

    @Override
    public void like(Integer studentId, Integer lectureId) {
        String updateStudentLectureLikeHql = "update StudentLecture sl set sl.like=1 where sl.student.id=" + studentId + " and sl.lecture.id=" + lectureId;
        String updateLectureLikeNumberHql = "update Lecture l set l.likeNumber=l.likeNumber+1 where l.lecture.id=" + lectureId;
        this.updateByHql(updateStudentLectureLikeHql);
        this.updateByHql(updateLectureLikeNumberHql);
    }

}
